package com.ruoyi.psi.purchase.purchasereturn.service;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDomain;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDto;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import com.ruoyi.psi.purchase.purchasereturn.domain.PurchaseReturn;
import com.ruoyi.psi.purchase.purchasereturn.domain.PurchaseReturnList;
import com.ruoyi.psi.purchase.purchasereturn.mapper.PurchaseReturnMapper;
import com.ruoyi.utils.ID.IdUtil;
import com.ruoyi.utils.UtilsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 采购退货
 * 业务处理层
 * 修改：lsy 2021.8.20
 */
@Service
public class PurchaseReturnServiceImpl implements IPurchaseReturnService {
    @Autowired
    private PurchaseReturnMapper purchaseReturnMapper;
    @Autowired
    private WarehouseMapper warehouseMapper; //仓库
    @Autowired
    private IdUtil idUtil;


    /**
     * 采购订单退货查询
     *
     * @param purchaseReturn
     * @return
     */
    @Override
    public List<PurchaseReturn> selectPurchaseReturnList(PurchaseReturn purchaseReturn) {
        List<PurchaseReturn> purchaseReturns = purchaseReturnMapper.selectPurchaseReturnList(purchaseReturn);
        for (int i = 0; i < purchaseReturns.size(); i++) {
            if (StringUtils.isNotNull(purchaseReturnMapper.selectPurchaseReturnListListById(purchaseReturns.get(i).getPurchaseReturnId()))) {
                //存在，则存入采购订单退货对应产品信息
                purchaseReturns.get(i).setPurchaseReturnLists(purchaseReturnMapper.selectPurchaseReturnListListById(purchaseReturns.get(i).getPurchaseReturnId()));
            }
        }
        return purchaseReturns;
    }

    /**
     * 新增时获取入库单详细
     *
     * @param purchaseStorageId
     * @return
     */
    @Override
    public List<PurchaseReturnList> getPurchaseStorageForReturn(String purchaseStorageId) {
        List<PurchaseReturnList> purchaseReturnLists = purchaseReturnMapper.getPurchaseStorageForReturn(purchaseStorageId);
        for (int i = 0; i < purchaseReturnLists.size(); ) {
            BigDecimal returnNum = new BigDecimal(purchaseReturnLists.get(i).getPurchaseReturnNumber());
            //减去已退回数量（初始填入的值）
            returnNum = returnNum.subtract(new BigDecimal(purchaseReturnMapper.getPurchaseStorageReturnNumber(purchaseStorageId, purchaseReturnLists.get(i).getProduceId())));
            if (returnNum.doubleValue() <= 0) {
                purchaseReturnLists.remove(i);
            } else {
                //存入
                purchaseReturnLists.get(i).setPurchaseReturnNumber(returnNum.doubleValue());
                //存入此时对应的退回价格
                purchaseReturnLists.get(i).setPurchaseReturnPrice(returnNum.multiply(new BigDecimal(purchaseReturnLists.get(i).getPurchaseReturnUnitPrice())).doubleValue());
                i++;
            }
        }
        return purchaseReturnLists;
    }

    /**
     * 按编号查询
     *
     * @param purchaseReturnId 采购订单退货编号
     * @return
     */
    @Override
    public PurchaseReturn selectPurchaseReturnById(String purchaseReturnId) {
        return purchaseReturnMapper.selectPurchaseReturnById(purchaseReturnId);
    }

    /**
     * 采购订单退货新增
     *
     * @param purchaseReturn 采购入库单据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPurchaseReturn(PurchaseReturn purchaseReturn) throws Exception {
        String purchaseReturnId = idUtil.createPurchaseReturnId();//自动生成编号
        if (StringUtils.isNotNull(purchaseReturnMapper.selectPurchaseReturnById(purchaseReturnId))) {
            //存在
            return 0;
        }
        //不存在
        purchaseReturn.setPurchaseReturnId(purchaseReturnId);//主表添加编号
        purchaseReturnMapper.insertPurchaseReturn(purchaseReturn);//采购订单退货据
//        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(purchaseReturn.getWarehouseId()); // 查询该仓库的产品列表
        List<PurchaseReturnList> purchaseReturnLists = purchaseReturn.getPurchaseReturnLists();//采购订单退货产品信息
        for (int i = 0; i < purchaseReturnLists.size(); i++) {
            purchaseReturnLists.get(i).setPurchaseReturnId(purchaseReturnId);//子表添加编号
//            WarehouseListDomain warehouseListDomain = new WarehouseListDomain(); // 仓库子表的对象
//            warehouseListDomain.setWarehouseId(purchaseReturn.getWarehouseId());
//            warehouseListDomain.setProduceId(purchaseReturnLists.get(i).getProduceId());
//            for (WarehouseListDto s : warehouseListDtos) {
//                // 遍历仓库
//                if (purchaseReturnLists.get(i).getProduceId().equals(s.getProduceId())) {
//                    // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品入库的数量
//                    warehouseListDomain.setProductNumber(s.getProductNumber() - purchaseReturnLists.get(i).getPurchaseReturnNumber());
//                    break; //终止循环
//                }
//            }
//            // 更新库存
//            warehouseMapper.updateWarehouseList(warehouseListDomain);
            purchaseReturnMapper.insertPurchaseReturnList(purchaseReturnLists.get(i));//采购订单退货产品信息
        }
        return 1;
    }

    /**
     * 采购订单退货更新
     *
     * @param purchaseReturn 采购入库单据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePurchaseReturn(PurchaseReturn purchaseReturn) throws Exception {
        if (!StringUtils.isNotNull(purchaseReturnMapper.selectPurchaseReturnById(purchaseReturn.getPurchaseReturnId()))) {
            //不存在
            return 0;
        }
        //存在采购订单退货单据
        purchaseReturnMapper.updatePurchaseReturn(purchaseReturn);//采购订单退货单据
        List<PurchaseReturnList> purchaseReturnListOld = purchaseReturnMapper.selectPurchaseReturnListListById(purchaseReturn.getPurchaseReturnId());
        List<PurchaseReturnList> purchaseReturnListNew = purchaseReturn.getPurchaseReturnLists();//采购订单退货产品信息
//        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(purchaseReturn.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < purchaseReturnListNew.size(); i++) {
            PurchaseReturnList purchaseReturnList = purchaseReturnMapper.selectPurchaseReturnListById(purchaseReturn.getPurchaseReturnId(), purchaseReturnListNew.get(i).getProduceId());
            if (StringUtils.isNotNull(purchaseReturnList)) {
                for (int j = 0; j < purchaseReturnListOld.size(); j++) {
                    if (purchaseReturnListOld.get(j).getProduceId().equals(purchaseReturnList.getProduceId())) {
                        purchaseReturnListOld.remove(j);
                        break;
                    }
                }
                //存在采购订单退货详细
                //更新
//                // 新建仓库子表的对象
//                WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
//                warehouseListDomain.setWarehouseId(purchaseReturn.getWarehouseId());
//                warehouseListDomain.setProduceId(purchaseReturnListNew.get(i).getProduceId());
//                for (WarehouseListDto s : warehouseListDtos) {
//                    // 遍历仓库
//                    if (purchaseReturnListNew.get(i).getProduceId().equals(s.getProduceId())) {
//                        // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品出库时减少的数量 - 新修改的出库数量
//                        warehouseListDomain.setProductNumber(s.getProductNumber() + purchaseReturnList.getPurchaseReturnNumber() - purchaseReturnListNew.get(i).getPurchaseReturnNumber());
//                        break; //终止循环
//                    }
//                }
//                warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
                purchaseReturnMapper.updatePurchaseReturnList(purchaseReturnListNew.get(i));
            } else {//新增
                purchaseReturnMapper.insertPurchaseReturnList(purchaseReturnListNew.get(i));
            }
        }

        // 说明有删除的对象
        for (int i = 0; i < purchaseReturnListOld.size(); i++) {
//            // 新建仓库子表的对象
//            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
//            warehouseListDomain.setWarehouseId(purchaseReturn.getWarehouseId());
//            warehouseListDomain.setProduceId(purchaseReturnListOld.get(i).getProduceId());
//            for (WarehouseListDto s : warehouseListDtos) {
//                // 遍历仓库
//                if (purchaseReturnListOld.get(i).getProduceId().equals(s.getProduceId())) {
//                    // 删除时，产品库存数 = 原有库存 + 该产品出库时的库存数
//                    warehouseListDomain.setProductNumber(s.getProductNumber() + purchaseReturnListOld.get(i).getPurchaseReturnNumber());
//                    break; //终止循环
//                }
//            }
//            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
            //最后将要删除的子标记录删除
            purchaseReturnMapper.deletePurchaseReturnListById(purchaseReturnListOld.get(i).getPurchaseReturnId(), purchaseReturnListOld.get(i).getProduceId());
        }

        return 1;
    }

    /**
     * 更新采购订单退货删除标志位
     *
     * @param purchaseReturn 采购入库单据
     * @return
     */
    @Override
    public int updatePurchaseReturnDeleteFlag(PurchaseReturn purchaseReturn) {
//        List<PurchaseReturnList> purchaseReturnLists = purchaseReturn.getPurchaseReturnLists();// 被删除记录的所有子表记录
//        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(purchaseReturn.getWarehouseId()); // 查询该仓库的产品列表
//        for (int i = 0; i < purchaseReturnLists.size(); i++) {
//            // 新建仓库子表的对象
//            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
//            warehouseListDomain.setWarehouseId(purchaseReturn.getWarehouseId());
//            warehouseListDomain.setProduceId(purchaseReturnLists.get(i).getProduceId());
//            for (WarehouseListDto s : warehouseListDtos) {
//                // 遍历仓库
//                if (purchaseReturnLists.get(i).getProduceId().equals(s.getProduceId())) {
//                    // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品出库时减少的数量
//                    warehouseListDomain.setProductNumber(s.getProductNumber() + purchaseReturnLists.get(i).getPurchaseReturnNumber());
//                    break; //终止循环
//                }
//            }
//            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
//        }
        return purchaseReturnMapper.updatePurchaseReturnDeleteFlag(purchaseReturn);
    }

    /**
     * 采购订单退货详细查询
     *
     * @param purchaseReturnList 采购入库产品信息
     * @return
     */
    @Override
    public List<PurchaseReturnList> selectPurchaseReturnListList(PurchaseReturnList purchaseReturnList) {
        return purchaseReturnMapper.selectPurchaseReturnListList(purchaseReturnList);
    }

    /**
     * 查询采购订单退货对应的所有产品详细
     *
     * @param purchaseReturnId 采购订单退货编号
     * @return
     */
    @Override
    public List<PurchaseReturnList> selectPurchaseReturnListListById(String purchaseReturnId) {
        return purchaseReturnMapper.selectPurchaseReturnListListById(purchaseReturnId);
    }

    /**
     * 查询采购订单退货对应的指定产品详细
     *
     * @param purchaseReturnId 采购订单退货编号
     * @param produceId        产品编号
     * @return
     */
    @Override
    public PurchaseReturnList selectPurchaseReturnListById(String purchaseReturnId, String produceId) {
        return purchaseReturnMapper.selectPurchaseReturnListById(purchaseReturnId, produceId);
    }

}
